

# delim ;  
clear all;
set matsize 11000 ;
eststo clear ;
capture program drop add_lab_FE;
capture log close ; 
log using "sample_composition.log", text replace;


*----------------------------------------------------------------------------------;
*-------------------------****** OUTPUT SETTINGS *****---------------------;
*----------------------------------------------------------------------------------;

*settings some settings that are common for the set of plots on this dofile; 
local pdf_plot_settings = 	`"scale(*1.5) ymtick(##4) "';


**EXTRA LINES: \hline\hline \bigskip\\[6ex]) ; 
*settings for latex tables ;
		
		
		global prehead_start "prehead(\begin{table}[!htb]\centering 
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}  
		\caption{@title}
		\setlength{\tabcolsep}{1pt}";
		
		global prehead_start_longcol "prehead(\begin{table}[!htb]\centering 
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}  
		\caption{@title}
		\setlength{\tabcolsep}{7pt}";
		local font_size "";
		
		global prehead_end         "\begin{tabular}{l*{@span}{c}}\hline\hline)";
		
		global prehead_end_bigskip "\begin{tabular}{l*{@span}{c}}\hline\hline \bigskip\\[6ex])";
		
		global postfoot "postfoot(\hline\hline
		\multicolumn{@span}{m{0.9\textwidth}}{ \footnotesize @note. @starlegend. }
		\end{tabular}\end{table})";
		
		
		
		
		
			local tex_settigns  = `"
		${prehead_start}
		`font_size'
		${prehead_end}
		${postfoot}
		"';	


*xlabel(1980(5)2010);
*ymtick(-0.2(0.2)0.6) ; 
*ylabel(-0.2(0.2)0.6);
*ysc(r(-0.2 0.6));
*xmtick(1984(1)2008) ; 
*----------------------------------------------------------------------------------;
*----------------------------------------------------------------------------------;
**************************************;


use "../data/hpms_hwy_stats_88_08.dta", clear  ;


do "_setup_expenditure.do";
do "_cleaning_and_new_variables_sample.do";
do "_programs_sample";

** generate a variable of average number of years between maintenance in a segment;

bysort id (year): egen N_maint=total(I2_t);
bysort id (year): egen xx=count(year);
gen av_year_maint=xx/(N_maint+1);

label var av_year_maint "mean t for I in seg i";
tab av_year_maint;
drop  xx;

*------------------------------------------------; 
*regressions; 
*-----------------------------------------------;

tab year;
local listcontinous "c.time cont Baseline" ;
local listcontinous2 "c.time_+_c.time2 cont_2 " ;
local listdiscrete  "ib2.periods dum Periods" ;





gen no_trucks_aadt=aadt_10000 - trucks_av_10000;
label var no_trucks_aadt "No trucks AADT/10000";
label var trucks_av_10000 "Trucks AADT/10000";


local keep_rule "if 1==1 ";

local keep_rule_all "if 1==1 ";
local keep_rule_after1992 "if year>1992" ;

ren unionization_*   union_*;

tab year;


local time 	        = word("`listcontinous'",1) ;
local time `:subinstr local time `"_+_"' `" "', ';
local name 	        = word("`listcontinous'",2);
local title 	    = word("`listcontinous'",2);  
	
local titleD_rig         "Rigid surface";
local titlesh_elev_10m_s "Elevation" ;
local titlesh_urb_10m_s  "NLCD Urban";
local titleurban         "HPMS Urban";
local titleaadt_10000    "AADT";
local titleD_rig   		 "Rigid surface";
local titlesn_d     	 "Structural Number";
local titleaverage_grade_seg "Average Grade";
local titlesh_h20_10m_s  "NLCD Water";
local titleunion_Mem_Total    "Unionization";



local cluster_var   state_year ;

global main_var aadt_10000 urban sh_urb_10m_s D_rig sn_d union_Mem_Total; 
global appendix_var   average_grade_seg sh_elev_10m_s sh_h20_10m_s; 	


local tit_state "State";
local tit_state_year "State-Year";
global SE_note "Standard Errors in Parentheses Clustered at the `tit_`cluster_var'' Level";
						

foreach comp_var of global main_var			{;
				gen comp_temp = `comp_var';
				label var comp_temp "x"; //clear labels that cause formatting problems for estout ;20
				eststo ,prefix(`comp_var'_): reghdfe 	Diri  I2_t_exp_I2 c.I2_t_exp_I2#(`time') `time'
													c.comp_temp c.comp_temp#c.I2_t_exp_I2 c.comp_temp#c.I2_t_exp_I2#(`time') 
													 ,  absor( id)  cluster(`cluster_var');
				drop comp_temp;
				estfe . `comp_var'_*, labels( id "Segment id FE");
			
				};


return list;
				
	***** Main Table  ; 
				local tex_settigns  = `"
										${prehead_start}
										`font_size'
										${prehead_end_bigskip}
										${postfoot}
										"';	

	return list;
	tokenize `"$main_var"' ; //this will give me the name of the variables in order to put in the table titles;
	esttab  `1'* `2'* `3'* `4'* `5'* `6'*
		using "${output}/tables/Table4_sample_composition.tex", replace
		b(%8.2fc) se(%8.2fc)  sfmt(%8.2fc)
		order( I2_t_exp_I2)
		star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
		note( \noteSampleComposition ${SE_note} )
		indicate(   `r(indicate_fe)' )
		title("Composition effects resurfacing\label{tab:composition_Q}")
		mtitle(	"\LongHead{15ex}{`title`1''}"
				"\LongHead{15ex}{`title`2''}"
				"\LongHead{15ex}{`title`3''}"
				"\LongHead{15ex}{`title`4''}"
				"\LongHead{15ex}{`title`5''}"
				"\LongHead{15ex}{`title`6''}"
				)
		noomitted   nobaselevels nonumbers
			substitute(\_ _ 
			"c.comp_temp" "\(x^0\)"
			"comp_temp" "\(x^0\)"
			"I2_t=1" "\mathds{1}_{ist}(q)"
			"Segment id FE" 
			"\cline{2-7}Segment id FE"
			"time" "\(t\)"
			"$\\(t\)s$" "\(\times\)"
							)
		se  par  ar2  r2 label nogaps  drop(_cons) compress
		obslast stats(N, fmt(%9.0fc))
		`tex_settigns';	

	**** NEW VARIABLES (wrt table in the paper);
	return list;
	
	
	
foreach comp_var of global appendix_var{;
				gen comp_temp = `comp_var';
				label var comp_temp "x"; //clear labels that cause formatting problems for estout ;20
				eststo ,prefix(`comp_var'_): reghdfe 	Diri  I2_t_exp_I2 c.I2_t_exp_I2#(`time') `time'
													 c.comp_temp c.comp_temp#c.I2_t_exp_I2 c.comp_temp#c.I2_t_exp_I2#(`time') 
													 ,  absor( id)  cluster(`cluster_var');
				drop comp_temp;
				estfe . `comp_var'_*, labels(id "Segment id FE");
			
				};


return list;
				
	



log close;
exit;
